﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WXTao
{
    public partial class Form2 : Form
    {
        WeixinWeb weixin;
       
        public delegate void LoginCallback(string data);
        public delegate void MsgCallback(string data);
        public delegate void GroupCallback(string data);

        public LoginCallback login_cb;
        public MsgCallback msg_callback;
        public GroupCallback group_callback;

        Action<String> AsyncUIDelegate;
        private Dictionary<string, string> choose_dic = new Dictionary<string, string>();
        private Dictionary<string, string> groups_dic = new Dictionary<string, string>();

        public Form2()
        {
            InitializeComponent();

            AsyncUIDelegate = delegate (string n) { richTextBox1.Text = n; };

            login_cb = new LoginCallback(LoginCallback_F);
            msg_callback = new MsgCallback(MsgCallback_F);
            group_callback = new GroupCallback(GroupCallback_F);

            weixin = new WeixinWeb();
            loadERcode();
        }

        private void loadERcode()
        {
            string s = weixin.getUUID();
            if (s == "")
                return;
            try
            {
                this.pictureBox1.Image = null;
                this.pictureBox1.WaitOnLoad = false; //设置为异步加载图片
                this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                this.pictureBox1.LoadAsync("https://login.weixin.qq.com/qrcode/" + s);

               
                weixin.weixinLogin(login_cb);
            }
            catch
            {
                MessageBox.Show("网络连接失败！");
            }
        }

        void LoginCallback_F(string data)
        {
            if (data.Equals("success"))
            {
                richTextBox1.Invoke(AsyncUIDelegate, new object[] {"登陆成功"});
            }
            else if (data.Equals("loading"))
            {
                showPic(weixin.User.Head_img);
            }

        }

        void showPic(string pic_url)
        {
            Action<String> picDelegate = delegate (string n)
            {
                if (n.Equals(""))
                {
                    loadERcode();
                    return;
                }
                var tmpArr = n.Split(',');
                byte[] bytes = Convert.FromBase64String(tmpArr[1]);

                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    ms.Write(bytes, 0, bytes.Length);
                    pictureBox1.Image = Image.FromStream(ms, true);

                }

            };

            pictureBox1.Invoke(picDelegate, new object[] { pic_url });
        }
        
        void MsgCallback_F(string data)
        {
            richTextBox1.Invoke(AsyncUIDelegate,new object[] { weixin.User.Data });
        }

        void GroupCallback_F(string data)
        {
            richTextBox1.Invoke(AsyncUIDelegate, new object[] { weixin.User.Data });

            Action<WeixinUser> listDelegate = delegate (WeixinUser user)
            {
                this.listView1.Items.Clear();
                this.listView2.Items.Clear();

                groups_dic.Clear();
                choose_dic.Clear();

                this.listView1.View = View.List;
                this.listView1.BeginUpdate();
                foreach(var item in user.Groups)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = item.Key;
                    this.listView1.Items.Add(lvi);
                    groups_dic.Add(item.Key, item.Value);
                }
                
                this.listView1.EndUpdate();
            };

            listView1.Invoke(listDelegate, new object[] { weixin.User });

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //weixin.startGetMessage(msg_callback);
            weixin.getConetct(group_callback);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in listView1.SelectedItems)
            {
                choose_dic.Add(groups_dic.ElementAt(lvi.Index).Key, groups_dic.ElementAt(lvi.Index).Value);
                groups_dic.Remove(groups_dic.ElementAt(lvi.Index).Key);
                listView1.Items.RemoveAt(lvi.Index);
            }

            this.listView2.View = View.List;
            this.listView2.BeginUpdate();
            foreach (var item in choose_dic)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = item.Key;
                this.listView1.Items.Add(lvi);
            }
            this.listView2.EndUpdate();
        }


    }
}
